package com.xmx.confu_work.config;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * @Author: DingZhen
 * @Data:2024-04-24 23:27
 */
@Configuration
public class MyCorsConfig {

    @Bean
    public CorsFilter corsFilter(){
        CorsConfiguration configuration = new CorsConfiguration();
        //规定谁可以跨域
        configuration.addAllowedOrigin("http://localhost:9999");
        //规定跨域可以用cookie
        configuration.setAllowCredentials(true);
        //允许那些方法(get,post等)可以过去
        configuration.addAllowedMethod("*");
        //允许那些请求头进来
        configuration.addAllowedHeader("*");
        //过滤器,添加映射路径，拦截一切请求
        UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();
        //注册方法
        urlBasedCorsConfigurationSource.registerCorsConfiguration("/**",configuration);

        return new CorsFilter(urlBasedCorsConfigurationSource);
    }
}
